<template>
  <div class="login-page">
    <ValidateForm @form-submit="onFormSubmit">
      <div class="mb-3">
        <label for="exampleFormControlInput1" class="form-label"
          >邮箱地址</label
        >
        <ValidateInput
          :rules="emailRules"
          placeholder="请输入正确的电子邮箱地址"
          v-model="emailVal"
          @value="emailValue"
        ></ValidateInput>
      </div>
      <div class="mb-3">
        <label for="exampleFormControlTextarea1" class="form-label">密码</label>
        <ValidateInput
          :rules="passwordRules"
          placeholder="请输入密码"
          v-model="passwordVal"
          @value="passwordValue"
        ></ValidateInput>
      </div>
      <template #submit>
        <span class="btn btn-danger">登录</span>
      </template>
    </ValidateForm>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from "vue";
import ValidateForm from "../components/ValidateForm.vue";
import ValidateInput, { RulesProp } from "../components/ValidateInput.vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { PostProps } from "../store";
import createMessage from "../hook/createMessage";
export default defineComponent({
  name: "LoginIndex",
  components: {
    ValidateForm,
    ValidateInput,
  },
  setup() {
    const emailVal = ref("");
    const emailRules: RulesProp = [
      { type: "required", message: "电子邮箱地址不能为空" },
      {
        type: "email",
        message: "请输入正确的电子邮箱格式",
      },
    ];
    const passwordVal = ref("");
    const passwordRules: RulesProp = [
      { type: "required", message: "密码不能为空" },
    ];
    const emailValue = (val: string) => {
      emailVal.value = val;
    };
    const passwordValue = (val: string) => {
      passwordVal.value = val;
    };
    //登录
    const store = useStore()
    const router = useRouter()
    const onFormSubmit = (result: boolean) => {
      console.log("123", result);
      if (result) {
        const payload={
          email:emailVal.value,
          password:passwordVal.value
        }
        store.dispatch('loginAndFecth',payload).then(()=>{
          
        router.push("/");
          
        })

     
      }
    };
    return {
      emailRules,
      emailVal,
      emailValue,
      onFormSubmit,
      passwordVal,
      passwordRules,
      passwordValue,
      
    };
  },
});
</script>

<style scoped></style>
